package com.aiju.cash.ui.good;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.utils.T;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;

/**
 * @author: 佐为
 * @date: 2016-12-17 14:40
 * @version: V1.0
 * @Description: 描述
 */
public class GoodCodeActivity extends BaseTitleActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_good_code);
        setCenterTitle("扫描条形码");
        getSupportFragmentManager().beginTransaction().add(R.id.fl_my_container, getCaptureFragment()).commit();
    }

    /**
     * 二维码
     *
     * @return
     */
    private CaptureFragment getCaptureFragment() {
        CaptureFragment captureFragment = new CaptureFragment();
        CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);
        captureFragment.setAnalyzeCallback(analyzeCallback);
        return captureFragment;
    }

    /**
     * 二维码解析回调函数
     */
    private final CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Intent intent = getIntent();
            intent.putExtra("code", result);
            setResult(101, intent);
            finish();
        }

        @Override
        public void onAnalyzeFailed() {
            T.ToastShort("扫描失败，请重试");
        }
    };

}
